/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.mail.*;
import javax.swing.*;

/**
 * Dialog allowing to chooser which filters must be applied TODO : add a "edit"
 * button to edit a filter
 */
final class EditFilters extends JDialog {
	/** Language resource */
	private ResourceBundle msgBundle;

	private JPanel panel;

	private JLabel listLabel;
	private JList filtersList;
	private JScrollPane filtersPane;
	private Vector filtersNames;

	private JButton addFilter;
	private JButton remove;

	private JLabel selectedLabel;
	private JList selectedList;
	private JScrollPane selectedPane;
	private Vector selectedNames;

	private JButton newFilter;
	private JButton ok;
	private JButton cancel;

	private Profile profile;

	private EditFiltersListener listener;

	private Store store;

	private ArrayList filters;

	private boolean hasChanged;

	/**
	 * Constructor
	 * 
	 * @param store
	 *            link to the mail account
	 * @param profile
	 *            user profile
	 * @param msgBundle
	 *            language resource
	 */
	protected EditFilters(Store store, Profile profile, ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("EditFilters.frameTitle"), true);

		filters = new ArrayList();

		this.msgBundle = msgBundle;

		this.store = store;

		this.profile = profile;

		hasChanged = false;

		listener = new EditFiltersListener();

		panel = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();

		panel.setLayout(gridBag);

		Common.buildConstraints(constraints, 0, 0, 1, 1, 100, 100);
		newFilter = new JButton(msgBundle.getString("EditFilters.newFilterLabel"));
		newFilter.addActionListener(new createFilterListener());
		gridBag.setConstraints(newFilter, constraints);
		panel.add(newFilter);

		Common.buildConstraints(constraints, 0, 1, 1, 1, 100, 100);
		listLabel = new JLabel(msgBundle.getString("EditFilters.listLabelText"));
		gridBag.setConstraints(listLabel, constraints);
		panel.add(listLabel);

		Common.buildConstraints(constraints, 0, 2, 1, 2, 100, 100);

		filters = new ArrayList(this.profile.getFilters());

		filtersNames = new Vector();

		int size = filters.size();

		Filter f = null;

		for (int i = 0; i < size; i++) {
			f = (Filter) filters.get(i);
			filtersNames.add(f.getName());
		}

		filtersList = new JList(filtersNames);
		filtersList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		filtersPane = new JScrollPane(filtersList);
		gridBag.setConstraints(filtersPane, constraints);
		panel.add(filtersPane);

		Common.buildConstraints(constraints, 1, 2, 2, 1, 100, 100);
		addFilter = new JButton(msgBundle.getString("EditFilters.addFilterLabel"));
		addFilter.addActionListener(new addFilterListener());
		gridBag.setConstraints(addFilter, constraints);
		panel.add(addFilter);

		Common.buildConstraints(constraints, 1, 3, 2, 1, 100, 100);
		remove = new JButton(msgBundle.getString("EditFilters.removeLabel"));
		remove.addActionListener(new removeFilterListener());
		gridBag.setConstraints(remove, constraints);
		panel.add(remove);

		Common.buildConstraints(constraints, 3, 1, 1, 1, 100, 100);
		selectedLabel = new JLabel(msgBundle.getString("EditFilters.selectedLabelText"));
		gridBag.setConstraints(selectedLabel, constraints);
		panel.add(selectedLabel);

		Common.buildConstraints(constraints, 3, 2, 1, 2, 100, 100);

		selectedNames = new Vector();

		for (int i = 0; i < size; i++) {
			f = (Filter) filters.get(i);

			if (f.isActivated())
				selectedNames.add(f.getName());
		}

		selectedList = new JList();
		selectedList.setListData(selectedNames);
		selectedPane = new JScrollPane(selectedList);
		gridBag.setConstraints(selectedPane, constraints);
		panel.add(selectedPane);

		Common.buildConstraints(constraints, 1, 4, 1, 1, 100, 100);
		ok = new JButton("OK");
		ok.addActionListener(listener);
		gridBag.setConstraints(ok, constraints);
		panel.add(ok);

		Common.buildConstraints(constraints, 2, 4, 1, 1, 100, 100);
		cancel = new JButton(msgBundle.getString("EditFilters.cancelLabel"));
		cancel.addActionListener(listener);
		gridBag.setConstraints(cancel, constraints);
		panel.add(cancel);

		setContentPane(panel);
		setSize(700, 500);
		show();
	}

	/**
	 * Tells wether something has changed or not
	 * 
	 * @return boolean : if true, write modifications
	 */
	protected final boolean hasChanged() {
		return (hasChanged);
	}

	protected void setFilterActivated(String name, boolean b) {
		if (b)
			selectedNames.add(name);

		else
			selectedNames.remove(name);

		selectedList.setListData(selectedNames);

		int size = filters.size();

		Filter filter = null;

		for (int i = 0; i < size; i++) {
			filter = (Filter) filters.get(i);

			if (filter.getName().compareTo(name) == 0) {
				filters.remove(filter);
				filter.setActivated(b);
				filters.add(filter);

				hasChanged = true;

				break;
			}
		}
	}

	private final class addFilterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String f = (String) filtersList.getSelectedValue();

			if (f != null)
				setFilterActivated(f, true);
		}

	}

	private final class removeFilterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String f = (String) selectedList.getSelectedValue();

			if (f != null)
				setFilterActivated(f, false);
		}

	}

	private final class createFilterListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			CreateFilter newFilter = new CreateFilter(store, profile, msgBundle);
			Filter f = newFilter.getFilter();

			if (f != null) {
				filters.add(f);

				newFilter.dispose();

				filtersNames.add(f.getName());
				filtersList.setListData(filtersNames);

				hasChanged = true;
			}
		}

	}

	/** Listener for this class */
	private final class EditFiltersListener implements ActionListener {
		/**
		 * This method is invoked when an event is triggered
		 * 
		 * @param e
		 *            event
		 */
		public final void actionPerformed(ActionEvent e) {
			JButton b = (JButton) e.getSource();

			if (b == ok) {
				profile.setFilters(new Vector(filters));
				hide();
			}

			else if (b == cancel)
				hide();
		}
	}
}
